/**
 * 关于作用域和变量提升的面试题
 */

//1
function Foo(){
    getName = function(){
        console.log(1);
    };
    return this;
}
Foo.getName=function(){
    console.log(2);
}
Foo.prototype.getName = function(){
    console.log(3);
}
var getName = function(){
    console.log(4)
};
function getName(){
    console.log(5);
}
Foo.getName(); //2
getName(); //4
Foo().getName();//1
getName();//1
new Foo.getName();//2
new Foo().getName();//3
new new Foo().getName();//3
//2 4 1 1 2 3 3
//2
function A(){
    alert(1);
}
function Fn(){
    A=function(){
        alert(2);
    };
    return this;
}
Fn.A = A;
Fn.prototype = {
    A:()=>{
        alert(3);
    }
};
A();
Fn.A();
Fn().A();
new Fn.A();
new Fn().A();
new new Fn().A();
//3
var x = 2;
var y = {
    x:3,
    z:(function(x){
        this.x*=x;
        x+=2;
        return function(n){
            this.x*=n;
            x+=3;
            console.log(x);
        }
    })(x)
};
var m = y.z;
m(4);
y.z(5);
console.log(x,y.x);
//4
var x=0;
    y=1;
function fn(){
    x+=2;
    fn = function(y){
        console.log(y+(--x));
    };
    console.log(x,y);
}
fn(3);
fn(4);
console.log(x,y);
//4 a等于什么下面条件成立
/**
 * ==隐式转换规则
 * 对象==字符串，对象toString()变为字符串
 * null==undefined相等
 * NaN ==NaN不相等
 * 剩下的都是转换为数字
 */
//方法1:
var a = {
    i:0,
    toString(){
        return ++this.i;
    }
}
if(a==1&&a==2&&a==3){
    console.log(1);
}
//方法2:
var i = 1;
Object.defineProperty(window,'a',{
    get(){
        return i++;
    }
})
if(a==1&&a==2&&a==3){
    console.log(1);
}
//方法3:
var a = [1,2,3];
a.toString = a.shift;
if(a==1&&a==2&&a==3){
    console.log(1);
}
